<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<%@ include file="/pub/head_bootstrap.inc"%>
		<script type="text/javascript" src="${baseURL}/js/module/common_edit.js"></script>
	    <title>${module.moduleName}编辑 </title>
	    <style type="text/css">
			.l-panel{
				border: 0px;
			}
			
			.l-panel .l-toolbar{
				background: #fff;
			}
			
			.navigator-search{
				margin: 0px !important;
    			background: #fff;
			}
	    </style>
	</head>
	<body style="padding-bottom:41px;">
		<div id="tabcontainer" style="margin:3px;">
			<div title="基本信息" tabid="mainTab">
	        	<form id="mainform" method="post" class="edit-form">
		        	<div class="l-group">
						<span>基本信息</span>
					</div>
	        		<input name="moduleValues" id="moduleValues" type="hidden"/>
	        	</form> 
	     	</div>
	     	<c:if test="${id!=null}">
	     	<%@ include file="../selector_form.jsp"%>
	     	<c:forEach items="${module.childTab}" var="o">
	     		<div title="${o.title}" tabid="${o.tabid}">
		     		<div class="l-group">
						<span>${o.title}</span>
					</div>
					<form id="${o.moduleId}formsearch" method="post">
						<input type="hidden" name="${module.module}list" id="${module.module}list"
							class="field" />
						<div class="selector-form"></div>
					</form>
					<div id="${o.moduleId}" class="child-module-tab" style="background: #fff;">
					</div>
					<!-- 明细与列表在同一个页面 -->
					<div id="${o.moduleId}detail" style="display:none;">
					 	<form id="${o.moduleId}formdetail" name="formdetail" method="post"></form>
					</div>
				</div>
	     	</c:forEach>
	     	</c:if>
		</div>
		<script type="text/javascript">
  			var pkId = '${id}';
           	//表单底部按钮 
       		LG.setFormDefaultBtn(FF_Cancel, FF_Save);
           	
       		//编辑，新增
	  		var mainform = $("#mainform");
      		var columns = 2;
	  		var dicObj = '${dic}';
	  		if(dicObj){
	  			dicObj = JSON.parse(dicObj);
	  		}
	  		var fieldsData = '${module.moduleFields}';
	  		var mainEditor = null;
	  		var win = parent || window;
	  		
	  		// 动态加载js脚本文件
	 		function loadScript(url,callback) {
	 			var script = document.createElement("script");
	 			script.type = "text/javascript";
	 			script.src = url;
	 			if ((navigator.userAgent.indexOf('MSIE') >= 0) 
	 				    && (navigator.userAgent.indexOf('Opera') < 0)){
	 				script.onreadystatechange = function () {
	 					var state = link.readyState;
	 					if (state === 'loaded' || state === 'complete' ) {
		 					link.onreadystatechange = null;
		 					callback();
	 					}
 					}
	 			}else{
	 				script.onload = callback;
	 			}
	 			document.head.appendChild(script);
	 		}
		    var eventObj = {};
	  		<c:if test="${module.editedJs!=null}">
	  		//加载编辑的事件js，因此要放在eventObj变量之后
	  		if('${module.editedJs}'!=''){
		  		loadScript('${baseURL}/${module.editedJs}',function(){
		  			mainEditor = initForm(fieldsData, mainform,dicObj, {'width':230, 'prefix':'', 'suffix':''},columns, eventObj.mainTab);
		  			if (pkId){
		     			getEditData();
		     		}
		  		});
	  		}else{
	  			mainEditor = initForm(fieldsData, mainform,dicObj, {'width':230, 'prefix':'', 'suffix':''},columns, eventObj.mainTab);
	  			if (pkId){
	     			getEditData();
	     		}
	  		}
	  		</c:if>
	  		<c:if test="${module.editedJs==null}">
	  		mainEditor = initForm(fieldsData, mainform,dicObj, {'width':230, 'prefix':'', 'suffix':''},columns, eventObj.mainTab);
	  		if (pkId){
     			getEditData();
     		}
	  		</c:if>
	  		
	  		//添加验证
     		jQuery.metadata.setType("attr", "validate"); 
     		LG.validate(mainform);
     		
			//保存
			function FF_Save(){
				if(eventObj.mainTab && eventObj.mainTab.beforeSave){
					eventObj.mainTab.beforeSave();
				}else{
					if (fieldsData) {
						var data = JSON.parse(fieldsData);
						var moduleValues = {};
						for ( var key in data ) {
							if(data[key].moduleId){
								if(!moduleValues[data[key].moduleId]){
									moduleValues[data[key].moduleId] = {};
								}
								var value = $("input[name='"+mainEditor.options.prefix+data[key].valueFieldId+"']").val();
								if(value){
									var values = value.split(";");
									for(var i=0;i<values.length;i++){
										if(!moduleValues[data[key].moduleId][i]){
											moduleValues[data[key].moduleId][i] = {};
										}
										moduleValues[data[key].moduleId][i][data[key].name] = values[i];
									}
								}
							}
						}
						$("#moduleValues").val(JSON.stringify(moduleValues));
					}
				}
				mainform.attr("action","${baseURL}/jdbc/common/basecommonsave/save.do?id=${id}&mdId=${param.mdId}&strategy=${strategy}");
				LG.submitForm(mainform, function (data) {
					if (data && data.hasOk) { 
						pkId = data.bean.id;
						win.$.ligerDialog.success('操作成功!');
						var iframe = $("#${param.menuId}");
				        if (iframe && iframe.FF_Reload)
				        {
				            iframe.FF_Reload();
				        }
				        window.location.href = '${baseURL}/jdbc/common/basecommonedit/edit.do?mdId=${param.mdId}&menuId=${param.menuId}&id=' + pkId;
	                } else { 
	                	win.$.ligerDialog.error("操作失败!失败信息如下:\n" + data.message + "\n如您对以上信息有疑问，请联系管理人员!");
	                }
				});
    		}
			
     		//取消按钮，即关闭窗体
		    function FF_Cancel(){
		        win.LG.closeCurrentTab("${module.module}Edit");
		    }
     		
     		//加载完数据后，在页面设置某些值
     		function FF_AfterLoaded(){
				//do noting
     		}
     		
     		function getEditData() {
     			$.ajax({
     				url : '${baseURL}/jdbc/common/basecommonedit/editJSON.do?id=${id}&mdId=${module.moduleId}&strategy=${strategy}',
     				type : 'post',
     				dataType : 'json',
     				success : function(data){
     					if(data) {
     						var form = mainEditor.form;
     						var fields = mainEditor.fields;
     						if (data) {
     							for ( var key in fields) {
     								var name = fields[key].code;
     								var value = null;
     								if(data[name]){
     									value = data[name].value;
     								}
     								if(fields[key].attr.moduleId){
     									value = "";
     									for(var k in data[fields[key].attr.moduleId]){
     										if(data[fields[key].attr.moduleId][k][fields[key].code]){
     											if(value==""){
	     											value += data[fields[key].attr.moduleId][k][fields[key].code].value;
	     										}else{
	     											value += ";" + data[fields[key].attr.moduleId][k][fields[key].code].value;
	     										}
     										}
     									}
     								}
     								if(mainEditor.element[name]){
     									if((mainEditor.element[name].type == "comboBox" || mainEditor.element[name].type == "checktree") && fields[key].options.valueFieldID){
     										var valueFieldId = fields[key].options.valueFieldID.replace(mainEditor.options.prefix,"");
     										if(data[valueFieldId]){
     											value = data[valueFieldId].value;
     										}
     									}
     									if(value!=null && value!=""){
     										if(mainEditor.element[name].type == "radio"){
     											mainEditor.element[name].element.clear();
     										}
     										mainEditor.element[name].element._setValue(value);
     									}else{
     										if(mainEditor.element[name].type != "radio"){
     											mainEditor.element[name].element.clear();
     										}
     									}
     								}else{
     									if(fields[key].type == "textarea"){
     										$("textarea[name='"+ mainEditor.options.prefix + name + "']",form).not(":checkbox").not(":radio").val(value);
     									}else{
     										$("input[name='"+ mainEditor.options.prefix + name + "']",form).not(":checkbox").not(":radio").val(value);
     									}
     								}
     							}
     						}
     						
     						if(eventObj.mainTab && eventObj.mainTab.initForm){
     							eventObj.mainTab.initForm(data);
     						}
     						<c:if test="${id!=null}">
     						getChild(data);
     						</c:if>
     					}
     				}
     			});
     		}
     		
  		</script>
  		<c:if test="${id!=null}">
  			<%@ include file="xnottabedit_child_list.jsp"%>
  			<%@ include file="xnottabedit_child_detail.jsp"%>
  		</c:if>
	</body>
</html>